﻿using HANS.DataBaseTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HANS_BackStage.Model.DbModel;
using HANS_BackStage.Model.WebModel;
using HANS_BackStage.Common;
using System.Web.Mvc;
using HANS_BackStage.Model;

namespace HANS_BackStage.Impl
{
    public  class AccountServices
    {

        private static  HANS_BackStage.DAL.BackStageDataEntityDAL<Backstage_role> roleDal = new DAL.BackStageDataEntityDAL<Backstage_role>();
        private static HANS_BackStage.DAL.BackStageDataEntityDAL<Backstage_log> logDal = new DAL.BackStageDataEntityDAL<Backstage_log>();

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Login_admin"></param>
        /// <returns></returns>
        public static Msg  Login(HANS_BackStage.Model.WebModel.HANS_Login Login_admin) {
            Msg msg = new Msg();
            msg = AdminServices.validate(Login_admin);
            if (msg.Result)
            {
                List<Backstage_role> roles = new List<Backstage_role>();
                List<Perissions> permissionsList = new List<Perissions>();
                Backstage_admin admin = msg.MsgObjectContent as Backstage_admin;
                roles = Role_PermiServices.GetRoles(Role_PermiServices.GetRoles(admin.Id));
                if (roles.Find(r=>r.Id.Equals(Login_admin.RoleTye))!=null)
                {
                    foreach (var item in roles)
                    {
                        Perissions per = new Perissions(item)
                        {
                            permissions = Role_PermiServices.GetPermissions(Role_PermiServices.GetPermissions(item.Id)),
                        };
                        permissionsList.Add(per);
                    }
                    Login_AdminInfo loginInfo = new Login_AdminInfo(admin, roles, permissionsList, Login_admin.RoleTye);
                    Login_SeesionHelper.SetInfoCache(loginInfo,Login_admin);
                    AdminServices.UpdateLastLoginDate(admin);
                    BackStage_Log(loginInfo, loginInfo.Name + "登录系统，登录时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), LogEnum.Login);
                    msg.ClearMsg();
                    msg.AddMsg("欢迎您登陆本平台！");

                }
                else
                {
                    msg.Result = false;
                    msg.AddMsg("您并未授予该角色！");
                }
                
            }
            return msg;

        }


        /// <summary>
        /// 返回登录Model
        /// </summary>
        /// <returns></returns>
        public static HANS_Login SetLoginViewModel(string url) {
            HANS_Login login = new HANS_Login()
            {
                ReturnUrl=url,
                Roles = roleDal.List().Select(r => new SelectListItem()
                {
                    Value = r.Id,
                    Text=r.Rolename
                }).ToList()
            };
            return login;
        }

        /// <summary>
        /// 添加记录日志
        /// </summary>
        /// <param name="Info"></param>
        /// <param name="content"></param>
        /// <param name="log_type"></param>
        public static void BackStage_Log(Login_AdminInfo Info,string content , LogEnum log_type ) {
            Backstage_log log = new Backstage_log()
            {
                Id = System.Guid.NewGuid().ToString(),
                Operationid = Info.adminEntity.Id,
                Type = (sbyte)log_type,
                Description = content,
                Operationdate = DateTime.Now
            };
            logDal.Insert(log);

        }
    }
}
